package com.wenwei.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.wenwei.bean.UserCouponBean;
import com.wenwei.bean.UserInfoBean;
import com.wenwei.net.ApiManager;
import com.wenwei.net.BaseSubscriber;
import com.wenwei.parking.BaseActivity;
import com.wenwei.parking.Constant;
import com.wenwei.parking.R;
import com.wenwei.utils.RogerSPUtils;
import com.wenwei.utils.UserStringUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/8/21.
 */
public class WalletActivity extends BaseActivity {
    @Bind(R.id.base_title)
    TextView baseTitle;
    @Bind(R.id.user_balance_tv)
    TextView userBalanceTv;
    @Bind(R.id.you_hui_tv)
    TextView youHuiTv;

    @Override
    public int setLayoutResourceId() {
        return R.layout.activity_wallet;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void initView() {
        super.initView();
        baseTitle.setText("我的钱包");

        UserInfoBean ub = RogerSPUtils.getObject(mContext, Constant.SP_USER_USER_RESULT, UserInfoBean.class);
        if (ub != null) {
            userBalanceTv.setText(""+ub.getBalance());
        }

    }

    @OnClick({R.id.base_back, R.id.zhi_fu_rl, R.id.yu_e_rl, R.id.you_hui_rl, R.id.fa_piao_rl})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.base_back:
                finish();
                break;
            case R.id.zhi_fu_rl:
                goActivity(mContext, BindPayActivity.class);
                break;
            case R.id.yu_e_rl:
                goActivity(mContext, BalanceActivity.class);
                break;
            case R.id.you_hui_rl:
                if(mList !=null ){
                    Bundle b = new Bundle();
                    b.putSerializable("bean",mList);
                    b.putInt("type",Constant.COUPON_USER);
                    goActivity(mContext, CouponActivity.class,b);
                }

                break;
            case R.id.fa_piao_rl:
                goActivity(mContext, InvoiceActivity.class);
                break;
        }
    }

    @Override
    protected void initData() {
        super.initData();
        getUserCoupon();
    }

    private ArrayList<UserCouponBean> mList;

    //用户优惠券
    private BaseSubscriber<ArrayList<UserCouponBean>> couponSb;
    private void getUserCoupon(){
        couponSb = new BaseSubscriber<ArrayList<UserCouponBean>>(mContext) {
            @Override
            public void onSuccess(ArrayList<UserCouponBean> userCouponBeen) {
                mList = userCouponBeen;
                youHuiTv.setText(userCouponBeen.size()+"张");
            }
        };
        ApiManager.getInstance().getUserCoupon(couponSb, UserStringUtils.getUserId(mContext));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(String event) {
        if(event.equals("999")){

            getUserInfo();
        }
    }

    private BaseSubscriber<UserInfoBean> infoSb;
    private void getUserInfo(){
        infoSb = new BaseSubscriber<UserInfoBean>(mContext) {
            @Override
            public void onSuccess(UserInfoBean userInfoBean) {
                Logger.d("重新获取用户信息+"+userInfoBean.getAvator());
                RogerSPUtils.putObject(mContext, Constant.SP_USER_USER_RESULT,userInfoBean);
                UserInfoBean ub = RogerSPUtils.getObject(mContext, Constant.SP_USER_USER_RESULT, UserInfoBean.class);
                if (ub != null) {
                    userBalanceTv.setText(""+ub.getBalance());
                }
            }
        };
        ApiManager.getInstance().getUserInfo(infoSb, UserStringUtils.getUserId(mContext));
    }
}
